package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.exception.checkGroupException;
import com.itheima.exception.checkItemException;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupAndCheckItem;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CheckGroupServiceImpl implements CheckGroupService {
    private final CheckGroupMapper checkGroupMapper;
    private final CheckItemMapper checkItemService;

    /**
     * 添加检查组
     *
     * @param checkGroup
     * @param checkItemIds
     */
    @Override
    @Transactional
    public void addcheckGroup(CheckGroup checkGroup, List<Integer> checkItemIds) {
        // 添加检查组
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        // 建立检查组与检查项的关系
        if (checkItemIds != null && !checkItemIds.isEmpty()) {
            for (Integer checkItemId : checkItemIds) {
                CheckGroupAndCheckItem relation = new CheckGroupAndCheckItem();
                relation.setCheckGroupId(checkGroupId);
                relation.setCheckItemId(checkItemId);
                checkGroupMapper.insert(relation);
            }
        }
    }


    /**
     * 检查组分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deletecheckGroup(Long id) {
        List<Long> setMealId = checkGroupMapper.findCheckGroupIdBySetMealId(id);
        if (!setMealId.isEmpty() || setMealId.size() > 0) {
            throw new checkItemException(MessageConstant.DELETE_CHECKGROUP_NOT_NULL);
        }
        checkGroupMapper.deletecheckGroup(id);
    }

    @Override
    public List<Long> findCheckGroupIdsBySetmealId(Long setmealId) {
        List<Long> ids = checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        if (ids == null || ids.isEmpty()) {
            throw new checkItemException(MessageConstant.QUERY_CHECKITEM_NOT_NULL);
        }
        return ids;
    }

    @Override
    public List<CheckGroup> findByIdcheckGroup(Long id) {
        List<CheckGroup> checkGroups = checkGroupMapper.findById(id);
        if (checkGroups == null || checkGroups.isEmpty()) {
            throw new checkGroupException(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return checkGroups;
    }

    @Override
    public List<CheckItem> findByAllcheckGroup() {
        return checkGroupMapper.findByAllCheckGroup();
    }

    @Override
    public void updatecheckGroup(CheckGroup checkGroup, List<Integer> checkItemIds) {
        // 更新检查组基本信息
        checkGroupMapper.updateCheckGroup(checkGroup);
        // 删除原有的关系
        checkGroupMapper.deleteReklation(checkGroup.getId());

        // 建立新的关系
        for (Integer checkItemId : checkItemIds) {
            CheckGroupAndCheckItem relation = new CheckGroupAndCheckItem();
            relation.setCheckGroupId(checkGroup.getId());
            relation.setCheckItemId(checkItemId);
            checkGroupMapper.insert(relation);
        }
    }
}
